package com.fysports.web.kit.commons;

import com.jfinal.log.Log;
import com.fysports.framework.exception.FrameException;
import org.bouncycastle.util.encoders.Base64;

import java.io.*;


/**
 * Created by konbluesky
 * Date : 14-8-2 下午3:05
 * Project : JdmData_WebServer
 */
public class StringKit extends com.jfinal.kit.StrKit {
    @SuppressWarnings("unused") private static Log log = Log.getLog(StringKit.class);

    /**
     * 常用正则表达式
     */
    public final static String regExp_integer_1 = "^\\d+$"; // 匹配非负整数（正整数 + 0）

    public final static String regExp_integer_2 = "^[0-9]*[1-9][0-9]*$"; // 匹配正整数

    public final static String regExp_integer_3 = "^((-\\d+) ?(0+))$"; // 匹配非正整数（负整数  + 0）

    public final static String regExp_integer_4 = "^-[0-9]*[1-9][0-9]*$"; // 匹配负整数

    public final static String regExp_integer_5 = "^-?\\d+$"; // 匹配整数

    public final static String regExp_float_1 = "^\\d+(\\.\\d+)?$"; // 匹配非负浮点数（正浮点数 + 0）

    public final static String regExp_float_2 =
            "^(([0-9]+\\.[0-9]*[1-9][0-9]*) ?([0-9]*[1-9][0-9]*\\.[0-9]+) ?([0-9]*[1-9][0-9]*))$"; // 匹配正浮点数

    public final static String regExp_float_3 = "^((-\\d+(\\.\\d+)?) ?(0+(\\.0+)?))$"; // 匹配非正浮点数（负浮点数 + 0）

    public final static String regExp_float_4 =
            "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*) ?([0-9]*[1-9][0-9]*\\.[0-9]+) ?([0-9]*[1-9][0-9]*)))$";
            // 匹配负浮点数

    public final static String regExp_float_5 = "^(-?\\d+)(\\.\\d+)?$"; // 匹配浮点数

    public final static String regExp_letter_1 = "^[A-Za-z]+$";// 匹配由26个英文字母组成的字符串

    public final static String regExp_letter_2 = "^[A-Z]+$";// 匹配由26个英文字母的大写组成的字符串

    public final static String regExp_letter_3 = "^[a-z]+$";// 匹配由26个英文字母的小写组成的字符串

    public final static String regExp_letter_4 = "^[A-Za-z0-9]+$";// 匹配由数字和26个英文字母组成的字符串

    public final static String regExp_letter_5 = "^\\w+$";// 匹配由数字、26个英文字母或者下划线组成的字符串

    public final static String regExp_email = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"; // 匹配email地址

    public final static String regExp_url_1 = "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
            // 匹配url

    public final static String regExp_url_2 = "[a-zA-z]+://[^\\s]*"; // 匹配url

    public final static String regExp_chinese_1 = "[\\u4e00-\\u9fa5]"; // 匹配中文字符

    public final static String regExp_chinese_2 = "[^\\x00-\\xff]"; // 匹配双字节字符(包括汉字在内)

    public final static String regExp_line = "\\n[\\s ? ]*\\r"; // 匹配空行：

    public final static String regExp_html_1 = "/ <(.*)>.* <\\/\\1> ? <(.*) \\/>/"; // 匹配HTML标记

    public final static String regExp_startEndEmpty = "(^\\s*) ?(\\s*$)"; // 匹配首尾空格

    public final static String regExp_accountNumber = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
            //匹配帐号是否合法(字母开头，允许5-16字节，允许字母数字下划线)

    public final static String regExp_telephone = "\\d{3}-\\d{8} ?\\d{4}-\\d{7}";
            //匹配国内电话号码，匹配形式如 0511-4405222 或 021-87888822

    public final static String regExp_qq = "[1-9][0-9]{4,}"; // 腾讯QQ号, 腾讯QQ号从10000开始

    public final static String regExp_postbody = "[1-9]\\d{5}(?!\\d)"; // 匹配中国邮政编码

    public final static String regExp_idCard = "\\d{15} ?\\d{18}"; // 匹配身份证, 中国的身份证为15位或18位

    public final static String regExp_ip = "\\d+\\.\\d+\\.\\d+\\.\\d+";//IP

    /**
     * 字符编码
     */
    public final static String encoding = "UTF-8";


    /**
     * Url Base64编码
     *
     * @param data 待编码数据
     * @return String 编码数据
     * @throws Exception
     */
    public static String encode(String data) throws Exception {
        // 执行编码

        byte[] b = Base64.encode(data.getBytes(encoding));

        return new String(b, encoding);
    }


    /**
     * Url Base64解码
     *
     * @param data 待解码数据
     * @return String 解码数据
     * @throws Exception
     */
    public static String decode(String data) throws Exception {
        // 执行解码

        byte[] b = Base64.decode(data.getBytes(encoding));

        return new String(b, encoding);
    }


    /**
     * Url Base64 解码
     *
     * @param data 待解码数据
     * @return byte[] 解码数据
     * @throws Exception
     */
    public static byte[] decodeBase64(String data) {
        return Base64.decode(data);
    }


    /**
     * Url Base64 编码
     *
     * @param data 待编码数据
     * @return byte[] 编码数据
     * @throws Exception
     */
    public static String encodeBase64(byte[] data) {
        return Base64.toBase64String(data);
    }


    /**
     * URL编码（utf-8）
     *
     * @param source
     * @return
     */
    public static String urlEncode(String source) {
        String result = source;
        try {
            result = java.net.URLEncoder.encode(source, encoding);
        }
        catch (UnsupportedEncodingException e) {
            log.error(e.getLocalizedMessage(),e);
            throw  new FrameException(e);
        }
        return result;
    }


    /**
     * 根据内容类型判断文件扩展名
     *
     * @param contentType 内容类型
     * @return
     */
    public static String getFileExt(String contentType) {
        String fileExt = "";
        if ("image/jpeg".equals(contentType))
            fileExt = ".jpg";
        else if ("audio/mpeg".equals(contentType))
            fileExt = ".mp3";
        else if ("audio/amr".equals(contentType))
            fileExt = ".amr";
        else if ("video/mp4".equals(contentType))
            fileExt = ".mp4";
        else if ("video/mpeg4".equals(contentType))
            fileExt = ".mp4";
        return fileExt;
    }


    /**
     * 查找vals中是否包含val
     * 包含条件为完全匹配，即：vals[0].equals(val)
     *
     * @param vals
     * @param val
     * @return
     */
    public static boolean hasVal(String[] vals, String val) {
        if (vals == null || vals.length == 0 || isBlank(val))
            return false;
        for (String tmp : vals) {
            if (tmp.trim().equals(val.trim())) {
                return true;
            }
        }
        return false;
    }


    /**
     * 查找vals中是否包含val
     * 包含条件为完全匹配，即：vals[0].indexOf(val) >= 0
     *
     * @param vals
     * @param val
     * @return
     */
    public static boolean containVal(String[] vals, String val) {
        if (vals == null || vals.length == 0 || isBlank(val))
            return false;
        boolean status = false;
        for (String s : vals) {
            if (val.indexOf(s) >= 0) {
                status = true;
            }
        }
        return status;
    }


    /**
     * 字符串不为 null 而且不为  "" 时返回 true
     */
    public static boolean notBlank(StringBuffer str) {
        return str == null || "".equals(str.toString().trim()) ? false : true;
    }


    /**
     * 获取bean名称
     *
     * @param bean
     * @return
     */
    public static String beanName(Object bean) {
        String fullClassName = bean.getClass().getName();
        String classNameTemp =
                fullClassName.substring(fullClassName.lastIndexOf(".") + 1, fullClassName.length());
        return classNameTemp.substring(0, 1) + classNameTemp.substring(1);
    }


    public static String FormatStackTrace(Throwable throwable) {
        if (throwable == null)
            return "";
        String rtn = throwable.getStackTrace().toString();
        try {
            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            throwable.printStackTrace(printWriter);
            printWriter.flush();
            writer.flush();
            rtn = writer.toString();
            printWriter.close();
            writer.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (Exception ex) {
        }
        return rtn;
    }


    public static byte[] HexStringToBytes(String paramString) {
        int i;
        byte[] arrayOfByte;
        int j;
        i = paramString.length() / 2;
        arrayOfByte = new byte[i];
        j = 0;
        do {
            try {
                arrayOfByte[j] = ((byte) Integer.parseInt(paramString.substring(j * 2, 2 + j * 2), 16));
                j++;
            }
            catch (Exception localException) {
                return null;
            }
        } while (j < i);
        return arrayOfByte;
    }


    public static String ByteArrayToHexString(byte[] paramArrayOfByte) {
        return toHexString(paramArrayOfByte);
    }


    public static String ByteArrayToHexString(String cmd) {
        if (cmd == null || cmd.length() == 0)
            return "";
        char[] chars = cmd.toCharArray();
        StringBuffer hex = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            hex.append(Integer.toHexString((int) chars[i]));
        }
        return hex.toString();
    }


    public static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException("this byteArray must not be null or empty");

        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if (i % 1 == 0)
                hexString.append(" ");
            if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }


    public static byte[] toByteArray(int iSource, int iArrayLen) {
        byte[] bLocalArr = new byte[iArrayLen];
        for (int i = 0; (i < 4) && (i < iArrayLen); i++) {
            bLocalArr[i] = (byte) (iSource >> 8 * i & 0xFF);
        }
        return bLocalArr;
    }


    /**
     * 单个 byte 转 int,
     *
     * @param b
     * @return
     */
    public static int toInt(byte b) {
        return Integer.parseInt(Integer.toHexString(b & 0xFF), 16);
    }


    /**
     * 单个 byte 转 int,不足两位则补0
     *
     * @param b
     * @return
     */
    public static String toIntStr(byte b) {
        int i = toInt(b);
        String s = "";
        if (i < 10) {
            s += "0" + i;
        }
        else {
            s = Integer.toString(i);
        }
        return s;
    }


    public static void main(String[] args) {
        System.out.println(toIntStr((byte) 06));
    }
}
